/***
 * * PROJECT
 * *    ATaG
 * * VERSION
 * *    $Revision: 1.2 $
 * * DATE
 * *    $Date: 2006/11/04 02:39:59 $
 * * LOG
 * *    $Log: SpeedLimitCalculator.java,v $
 * *    Revision 1.2  2006/11/04 02:39:59  lmottola
 * *    Implemented topology creation for SWANS.
 * *
 * *    Revision 1.1  2006/11/03 23:08:15  lmottola
 * *    Traffic application beta running.
 * *
 * *    Revision 1.1  2006/10/27 01:30:55  animesh
 * *    trafficapplication renamed to atag.trafficapplication
 * *
 * *    Revision 1.1  2006/10/27 01:28:04  animesh
 * *    Traffic Application Added.
 * *
 * *    Revision 1.2  2006/10/19 21:21:53  lmottola
 * *    Added CVS preamble.
 * *
 * *
 * *   ATaG (Abstract Task Graph) - Data-driven Macroprogramming for
 * *                                Sensor Networks
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/
package atag.apps.traffic;

import atag.runtime.*;

/**
 * 
 * @author Animesh
 */
public class SpeedLimitCalculator implements Runnable {

	private DataPool m_dataPool;

	private NodeInfo m_myState;

	private int speedLimit = 0;

	/** Creates a new instance of HelloWorldReader */
	public SpeedLimitCalculator(DataPool dp, NodeInfo myconfig) {
		m_dataPool = dp;
		m_myState = myconfig;
	}

	public void run() {
		DataItem t_dataItem = m_dataPool.getData(IDConstants.T_SLCALCULATOR,
				IDConstants.D_ASPEED);
		if (t_dataItem != null) {
			// I got a AvSpeed
			AverageSpeed recvdASpeed = (AverageSpeed) t_dataItem.core();
			this.handleAverageSpeedProduced(recvdASpeed);
		} else {
			// I got a AQlen
			t_dataItem = m_dataPool.getData(IDConstants.T_SLCALCULATOR,
					IDConstants.D_AQLENGTH);
			// I got a AvSpeed
			AverageQueueLength recvdAQLength = (AverageQueueLength) t_dataItem
					.core();
			this.handleAverageQueueLengthProduced(recvdAQLength);
		}
	}

	private void handleAverageSpeedProduced(AverageSpeed r_aSpeed) {
		int sourceID = r_aSpeed.getSource();
		double aspeed = r_aSpeed.getASpeed();

		m_myState.debugPrint("SpeedLimitCalculator: got a AverageSpeed from " + sourceID
				+ " saying the average speed is " + aspeed);

		// TODO do something proper with the ASpeed... now.. just create a
		// random Speed Limit and send out
		
		speedLimit++;
		m_myState.debugPrint("Generating a SpeedLimit data item "+speedLimit);
		SpeedLimit sLimit = new SpeedLimit(this.m_myState.getMyId(),
				speedLimit);
		DataItem m_dataitem = new DataItem(IDConstants.D_SLIMIT,
				IDConstants.T_SLCALCULATOR, sLimit);
		m_dataPool.putData(m_dataitem);

	}

	private void handleAverageQueueLengthProduced(AverageQueueLength r_aQLength) {
		
		int sourceID = r_aQLength.getSource();
		double aQlen = r_aQLength.getAQLength();

		m_myState.debugPrint("SpeedLimitCalculator: got a AverageQueueLength from " + sourceID
				+ " saying the average queue length is " + aQlen);

		// TODO do something proper with the AQlen... now.. just create a
		// random Speed Limit and send out

		speedLimit++;
		m_myState.debugPrint("Generating a SpeedLimit data item "+speedLimit);
		SpeedLimit sLimit = new SpeedLimit(this.m_myState.getMyId(),
				speedLimit);
		DataItem m_dataitem = new DataItem(IDConstants.D_SLIMIT,
				IDConstants.T_SLCALCULATOR, sLimit);
		m_dataPool.putData(m_dataitem);
	}

}
